<template>
  <section>
    <!--查询工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true">
        <el-form-item>
          <el-input v-model="keyword" placeholder="姓名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="keywordQuery">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>


    <!--列表-->
    <el-table :data="users" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
              style="width: 100%;" @cell-dblclick="detail">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="sn" label="产品" width="120" sortable>
      </el-table-column>
      <el-table-column prop="name" label="产品类型" width="130" sortable>
      </el-table-column>
      <el-table-column label="操作" >
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>


    <!--分页工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="total, sizes, prev, pager, next, jumper"
                     @size-change="handleSizeChange"
                     @current-change="handleCurrentChange"
                     :page-size="pageSize"
                     :page-sizes="[5,10,20,30,50]"
                     :total="total"
                     style="float:right;">
      </el-pagination>
    </el-col>


    <!--新增和编辑的界面
 打开嵌套表单
 :visible.sync="dialogFormVisible"
title的值动态获取，在点击新增就赋值为新增，点击修改就赋值修改

 -->
    <el-dialog :title=title :visible.sync="addFormVisible" :close-on-click-modal="false">
      <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">

        <el-form-item label="字典编号" prop="sn">
          <el-input v-model="addForm.sn" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="字典名" prop="name">
          <el-input v-model="addForm.name" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
      </div>
    </el-dialog>

    <el-drawer
        title="我是标题"
        :visible.sync="drawer"
        :with-header="false">
      <span>
        <!--查询工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">

        <el-form-item>
          <el-button type="success" @click="handleAdd1">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>


        <!--明细的-->
      <el-dialog :title=title1 :visible.sync="addFormVisible1" :close-on-click-modal="false" append-to-body>
        <el-form :model="editForm" label-width="80px" ref="addForm">

          <el-form-item label="明细名">
            <el-input v-model="editForm.name" auto-complete="off"></el-input>
          </el-form-item>
        </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="addFormVisible1 = false">取消</el-button>
        <el-button type="primary" @click.native="addSubmit1" :loading="addLoading">提交</el-button>
      </div>
    </el-dialog>


        <!--列表-->
    <el-table :data="deta" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
              style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="name" label="明细名称" width="120" sortable>
      </el-table-column>
<!--      <el-table-column prop="types_id" label="类型" width="120" sortable>-->
<!--      </el-table-column>-->
      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small" @click="Edit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handle1(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

        <!--分页工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove1" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="total, sizes, prev, pager, next, jumper"
                     @size-change="handleSizeChange1"
                     @current-change="handleCurrentChange1"
                     :page-size="pageSize1"
                     :page-sizes="[5,10,20,30,50]"
                     :total="total1"
                     style="float:right;">
      </el-pagination>
    </el-col>


      </span>
    </el-drawer>
  </section>
</template>

<script>
import util from '../../common/js/util'
//import NProgress from 'nprogress'
import {getUserListPage, removeUser, batchRemoveUser, editUser, addUser} from '../../api/api';

export default {
  data() {
    return {
      typee:[],//管理员下拉列表
      drawer: false,

      //查询相关
      keyword:'',

      filters1: {
        name: '',
        types_id:null,
      },
      users: [],
      deta: [],
      listLoading: true,
      sels: [],//列表选中列
      page: 1,//表示当前页或者需要跳转展示的页面   默认为1
      page1: 1,//表示当前页或者需要跳转展示的页面   默认为1
      total1: 0,//总条数  默认为1
      total: 0,  //总条数  默认为1
      pageSize: 10, //每页展示条数 默认10
      pageSize1: 10, //每页展示条数 默认10

      addFormVisible: false,//新增界面是否显示
      addFormVisible1: false,//新增界面是否显示

      addLoading: false, //加载效果
      //双向绑定新增界面数据
      addForm: {
        //部门编号
        sn: '',
        //部门名字
        name: '',

      },//双向绑定新增界面数据
      editForm: {
        //部门名字
        name: '',
        types_id:null,

      },
      //提示必须输入项
      addFormRules: {
        name: [
          {required: true, message: '请输入字典名称', trigger: 'blur'}
        ],
        sn: [
          {required: true, message: '请输入字典编号', trigger: 'blur'}
        ]
      },
      title: '',
      title1: '',
      typeid: null,



    }


  },
  methods: {
    detail(val) {
      //打开抽屉
      this.drawer = true;
      console.log(val.id)
      this.typeid = val.id
      // this.$http.get("/dicType/" + val.id).then(result => {
      //
      //   console.log(result)
      //   this.deta = result.data.detail;
      // })
      this.dataIndex1();
    },
    //高级查询
    keywordQuery(){
        this.currentPage = 1;
        this.dataIndex()
    },
    //删除明细
    handle1: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        console.log(row.id)
        console.log("row.id")
        //发送请求
        this.$http.delete("/deta/" + row.id).then(result => {
          console.log(result)
          //解构返回的值
          let {success, message} = result.data;
          //判断
          if (success) {
            // 将加载效果解除
            this.listLoading = false;
            //删除成功
            this.$message({
              message: '删除成功',
              type: 'success'
            });
            //  删除成功跳转第一页
            this.page = 1;
            //  重新加载
            this.dataIndex();
          } else {
            //  删除失败
            this.$message.error('网络延迟');
          }
          //  重新加载
          this.dataIndex1();
        })
      }).catch(() => {

      });
    },


    //删除类型
    handleDel: function (index, row) {
      this.$confirm('删除字典类型会同事删除字典内的明细，你确定要删除吗', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        console.log(row.id)
        //发送请求
        this.$http.delete("/dicType/" + row.id).then(result => {
          console.log(result)
          //解构返回的值
          let {success, message} = result.data;
          //判断
          if (success) {
            // 将加载效果解除
            this.listLoading = false;
            //删除成功
            this.$message({
              message: '删除成功',
              type: 'success'
            });
            //  删除成功跳转第一页
            this.page = 1;
            //  重新加载
            this.dataIndex();
          } else {
            //  删除失败
            this.$message.error('网络延迟');
          }
        })
      }).catch(() => {

      });
    },


    handleSizeChange(val) {
      this.pageSize = val;
      this.dataIndex();
    },
    handleSizeChange1(val) {
      this.pageSize1 = val;
      this.dataIndex1();
    },
    //发送请求，获取所有的员工信息
    loadEmployeees() {
      this.$http.get("/employee").then(result => {
        console.log(result)
        this.employees = result.data;
      })
    },
    //发送请求获取所有的级联相关的数据
    loadPraent() {
      this.$http.get("/department/cascade").then(result => {
        console.log(result);
        this.parentByDept = result.data;
      })
    },
    //显示编辑界面
    handleEdit(index, row) {
      this.title = "修改";
      //  回显数据
      this.addForm = Object.assign({}, row);
      //打开新增和修改的弹框
      this.addFormVisible = true;
    },
    //显示明细编辑界面
    Edit(index, row) {
      this.title1 = "编辑";
      //  回显数据
      this.editForm = Object.assign({}, row);
      //获取明细数据
      this.geTypee();
      //打开新增和修改的弹框
      this.addFormVisible1 = true;
    },
    //新增按钮触发执行
    handleAdd() {
      this.title = "新增";
      //清空表单的值
      this.addForm = {
        //字典编号
        sn: '',
        //字典名字
        name: '',
      };
      //打开新增和修改的弹框
      this.addFormVisible = true;
    },
    //新增按钮触发执行
    handleAdd1() {
      this.title1 = "新增";
      //清空表单的值
      this.editForm = {
        //明细名字
        name: '',
        types_id:null,
      };
      console.log("新增")
      //打开新增和修改的弹框
      this.addFormVisible1 = true;
    },

    //新增和编辑的模态框确定按钮触发
    addSubmit() {
      //这一句就是判断表单中必填的项目是否填了
      this.$refs.addForm.validate((valid) => {
        //如果填了
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            //打开加载效果
            this.addLoading = false;

            //NProgress.start();
            this.$http.put("/dicType", this.addForm).then((result) => {
              //解构传递过来的值
              let {success, message} = result.data;
              if (success) {
                //操作成功
                this.$message({
                  message: '操作成功',
                  type: 'success'
                });
                // 将加载效果解除
                this.listLoading = true;
                //操作成功关闭弹框
                this.addFormVisible = false;
                //  操作成功跳转第一页
                this.page = 1;
                //  重新加载页面
                this.dataIndex();
              } else {
                //  操作失败
                this.$message.error('网络延迟');
              }
            });
          });
        }
      });
    },

    //明细新增和编辑的模态框确定按钮触发
    addSubmit1() {
      //这一句就是判断表单中必填的项目是否填了
      this.$refs.addForm.validate((valid) => {
        //如果填了
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            //打开加载效果

            //NProgress.start();

            this.editForm.types_id =this.typeid ;
            this.$http.put("/deta", this.editForm).then((result) => {
              //解构传递过来的值
              let {success, message} = result.data;
              if (success) {
                //操作成功
                this.$message({
                  message: '操作成功',
                  type: 'success'
                });
                // 将加载效果解除

                //操作成功关闭弹框
                this.addFormVisible1 = false;
                //  操作成功跳转第一页
                this.page = 1;

              } else {
                //  操作失败
                this.$message.error('网络延迟');
              }
              //  重新加载页面
              this.dataIndex1();
            });
          });
        }
      });
    },


    //分页中点击跳转页数触发事件执行的方法
    handleCurrentChange(val) {
      console.log(val)
      this.page = val;

      this.dataIndex();
    }
    ,//分页中点击跳转页数触发事件执行的方法
    handleCurrentChange1(val) {
      console.log(val)
      this.page1 = val;
      this.dataIndex1();
    }
    ,
    //批量删除
    batchRemove() {
      //var ids = this.sels.map(item => item.id).toString();
      let ids = this.sels.map(item => item.id);
      console.log(ids)//获取一个数组  里面封装的是选择的数据的id值
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;//加载效果
        //NProgress.start();
        this.$http.patch("/dicType", ids).then((result) => {
          console.log(result)
          //解构返回的值
          let {success, message} = result.data;
          //判断
          if (success) {
            // 将加载效果解除
            this.listLoading = false;

            //删除成功
            this.$message({
              message: '删除成功',
              type: 'success'
            });
            //  删除成功跳转第一页
            this.currentPage = 1;
            //  重新加载
            this.dataIndex();
          } else {
            //  删除失败
            this.$message.error('网络延迟');
          }

        });
      }).catch(() => {

      });

    }
    ,


    //批量删除1
    batchRemove1() {
      //var ids = this.sels.map(item => item.id).toString();
      let ids = this.sels.map(item => item.id);
      console.log(ids)//获取一个数组  里面封装的是选择的数据的id值
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;//加载效果
        //NProgress.start();
        this.$http.patch("/deta", ids).then((result) => {
          console.log(result)
          //解构返回的值
          let {success, message} = result.data;
          //判断
          if (success) {
            // 将加载效果解除
            this.listLoading = false;

            //删除成功
            this.$message({
              message: '删除成功',
              type: 'success'
            });
            //  删除成功跳转第一页
            this.page1 = 1;
            //  重新加载
            this.dataIndex1();
          } else {
            //  删除失败
            this.$message.error('网络延迟');
          }

        });
      }).catch(() => {

      });

    }
    ,


    //将获取页面封装成一个方法
    dataIndex() {
      //需要传递到后端的参数
      let parm = {
        currentPage: this.page,
        pageSize: this.pageSize,
        keyword: this.keyword
      };
      // 发送请求
      this.$http.post("/dicType", parm).then(result => {
        console.log(result);
        this.users = result.data.data;
        this.total = result.data.totals;
        this.listLoading = false;
      })
    }
    ,
    //将获取页面封装成一个方法
    dataIndex1() {
      //需要传递到后端的参数
      let parm = {
        currentPage: this.page1,
        pageSize: this.pageSize1,
        keyword: this.keyword,
        typeid: this.typeid,
      };
      // 发送请求
      this.$http.post("/deta", parm).then(result => {
        console.log(result);
        this.deta = result.data.data;
        this.total1 = result.data.totals;
        this.listLoading = false;
      })
    }
    ,


    //性别显示转换
    formatSex(row, column) {
      return row.state == 0 ? '启用' : row.state == -1 ? '禁用' : '未知';
    }
    ,
    selsChange(sels) {
      this.sels = sels;
    },
    geTypee(){
        this.$http.get("/dicType").then(res=>{
                this.typee = res.data;
        })
    }
  },
  mounted() {
    this.dataIndex();
  }
}

</script>

<style scoped>

</style>